国内极速提速!彻底解决 apt install 下载慢、超时、卡死问题

19次阅读
没有评论

Linux 的小伙伴应该都有过这种崩溃时刻:

敲完 sudo apt install xxx 命令后,终端进度条龟速爬行,几十 KB/s 的速度让人抓狂,经常还会出现连接超时、下载失败、哈希校验错误等问题,明明网络正常,装个软件却要等十几分钟。

其实这不是你的服务器、电脑网络问题,根本原因是 Ubuntu/Debian 默认搭载的是国外官方源,国内访问延迟高、丢包严重。想要满血提速,只需切换国内高质量镜像源,轻松实现满速下载。

今天给大家分享一套零门槛、稳如泰山的 apt 国内加速方案,包含永久配置、临时加速、一键脚本,新手也能无脑操作。


一、先搞懂:为什么默认 apt 这么慢?

APT 是 Ubuntu、Debian 系列系统的默认包管理器,我们执行apt installapt update 时,系统会默认从archive.ubuntu.com 等国外服务器拉取软件包。

这类海外服务器没有国内节点,日常访问延迟极高,高峰期还会拥堵丢包,直接导致:

  • 软件包下载速度几十 KB/s
  • 更新索引超时、连接失败
  • 安装中断、依赖下载不全
  • Docker、开发环境等大型组件安装失败

解决核心:把默认国外源替换为清华、阿里云、中科大等国内公益镜像源,服务器就近访问,速度直接拉满。


二、高危操作必做:备份原始源文件

修改源文件前一定要备份!配置出错可一键还原,避免系统更新、安装软件异常,这是 Linux 改配置的基本好习惯。

终端直接执行备份命令:

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

后续配置出错,执行以下命令即可还原官方默认源:

sudo cp /etc/apt/sources.list.bak /etc/apt/sources.list

三、永久提速:替换国内顶级镜像源(推荐)

这里分享清华源阿里云源两套最稳定的配置,覆盖个人电脑、云服务器、虚拟机场景,大家按需选择即可。

先清空原有源文件内容:

sudo truncate -s 0 /etc/apt/sources.list

方案1:清华大学镜像源(稳定性拉满,首推)

清华镜像源更新及时、同步速度快,适配所有 Ubuntu 版本,个人和服务器通用,几乎无故障。

根据你的系统版本选择对应命令:

Ubuntu 22.04(jammy)

sudo bash -c "cat << EOF > /etc/apt/sources.list
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
EOF"

Ubuntu 24.04(noble)

sudo bash -c "cat << EOF > /etc/apt/sources.list
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-backports main restricted universe multiverse
EOF"

方案2:阿里云镜像源(云服务器专属优化)

阿里云源在国内云服务器(阿里云、腾讯云、华为云)环境下延迟更低,速度更稳,适合服务器部署场景。

Ubuntu 22.04 阿里源

sudo bash -c "cat << EOF > /etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
EOF"

四、生效配置,验证提速效果

替换完源文件后,必须更新软件索引缓存,新源才会生效:

sudo apt update

看到终端输出 mirrors.tuna.tsinghua.edu.cnmirrors.aliyun.com,说明配置成功。

顺带升级已有软件(可选,建议执行一次):

sudo apt upgrade -y

此时再执行 apt install,速度直接从几十 KB/s 飙升至满速,彻底告别卡顿超时。


五、临时加速技巧(不想改配置专用)

如果只是临时用一下,不想永久修改系统源文件,可以用 单次临时换源 方式,仅当前终端生效,重启失效:

通过 sed 命令临时替换源地址,一键生效:

# 临时替换为清华源
sudo sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
sudo apt update

想要恢复默认,重新执行备份还原命令即可,灵活又方便。


六、常见问题快速排查

1. 提示 GPG 密钥缺失/验证失败

国内源偶尔出现密钥校验问题,执行以下命令更新密钥即可修复:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 40976EAF437D05B5

2. 换源后速度还是慢

大概率是系统版本代号不匹配,检查自己的 Ubuntu 版本:lsb_release -a,核对源文件中的 jammy/noble 是否对应。

3. 部分第三方软件下载慢

Docker、Node、Python 等第三方源,需要单独配置对应国内镜像,系统 APT 源仅适配官方软件仓库。


七、总结

apt 下载慢从来不是网络问题,是源的问题。只需简单三步:备份源文件、替换国内镜像、更新缓存,就能一劳永逸解决卡顿、超时问题。

日常个人开发优先用清华源,云服务器部署优先阿里源,稳定、高速、零广告、永久免费,是国内 Linux 用户的最优解。

从此以后,apt install 全程满速,开发、部署效率直接翻倍✨


码字不易,有用点赞收藏,后续分享更多 Linux 提速小技巧!

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)
验证码